rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)


m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

m[, date := as.Date(date, "%d%b%Y")]
m[, dayofweek := wday(date)]

m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, bias := (memrableperiod_pret - memrableperiod_aret)]

m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, exretrecall_1day_rate := retrecall_1day_rate - ret_lagd1 * 100]
m[, exretrecall_1year_rate := retrecall_1year_rate - ret_lagy1 * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

C <- data.table(read_xlsx("../Data/AccountData.xlsx", guess_max = 1e4))
C[, aret_1day_v2 := as.numeric(aret_1day_v2) * 100]
C[, aret_0day_v2 := as.numeric(aret_0day_v2) * 100]
C[, aret_4_1week_v2 := as.numeric(aret_4_1week_v2) * 100]
C[, aret_ytd_v2 := as.numeric(aret_ytd_v2) * 100]
m <- merge(m, C, by = "id", all.x = T)

m[, aret_1day_v2 := Winsorize(aret_1day_v2, quantile(aret_1day_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_0day_v2 := Winsorize(aret_0day_v2, quantile(aret_0day_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_4_1week_v2 := Winsorize(aret_4_1week_v2, quantile(aret_4_1week_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_ytd_v2 := Winsorize(aret_ytd_v2, quantile(aret_ytd_v2, probs = c(0.05, 0.95), na.rm = T))]

###########################################################################
# Probed Recall: Table 5
###########################################################################

m[, ret_intraday2 := ret_intraday2 * 100]
m[, ret_lagmonth := (ret_lagw1 + ret_lagw2 + ret_lagw3 + ret_lagw4) * 100]
m[, exretrecall_30day_rate := retrecall_30day_rate - ret_lagmonth]
m[, fraction_up := word_count_up / (word_count_up + word_count_down + word_count_neutral)]

sample1 <- m[dayofweek >= 2 & dayofweek <= 6 & 
  !is.na(exretrecall_1day_rate) & !is.na(exretrecall_30day_rate) & 
  !is.na(ret_intraday2)]

sample2 <- sample1[!is.na(aret_1day_v2) & !is.na(aret_0day_v2)]

f1 <- felm(exretrecall_1day_rate ~ ret_intraday2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type | 0 | date, sample1)

f4 <- felm(exretrecall_30day_rate ~ ret_intraday2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type | 0 | date, sample1)

f2 <- felm(exretrecall_1day_rate ~ ret_intraday2 + aret_1day_v2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type| 0 | date, sample2)

f3 <- felm(exretrecall_1day_rate ~ aret_0day_v2 + aret_1day_v2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type| 0 | date, sample2)

f5 <- felm(exretrecall_30day_rate ~ ret_intraday2 + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type | 0 | date, sample2)

f6 <- felm(exretrecall_30day_rate ~ aret_0day_v2 + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
  accountcheck_freq + newscheck_freq + discussion_freq + num_wechat + type | 0 | date, sample2)

stargazer(f1, f4, f2, f3, f5, f6,
  align = TRUE, dep.var.labels.include = TRUE,
  covariate.labels = c("MktRet\\textsubscript{today}", "OwnRet\\textsubscript{today}", "OwnRet\\textsubscript{1D}", "OwnRet\\textsubscript{1M}"),
  omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
  title = "",
  single.row = FALSE, column.sep.width = "0pt", digits = 2
)